package com.example.test.mapper;

import com.example.test.bean.DTO.output.GoodsListOutputDTO;
import com.example.test.bean.Goods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 商品信息表 Mapper 接口
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    //根据keyword模糊查询
    @Select("SELECT\n" +
            "\tgoods_id,\n" +
            "\tgoods_name,\n" +
            "\tgoods_Type,\n" +
            "\tgoods_Braend,\n" +
            "\tgoods_Remain,\n" +
            "\tpurchase_price,\n" +
            "\tsell_price,\n" +
            "\tcreate_time\n" +
            "FROM\n" +
            "\tgoods\n" +
            "WHERE\n" +
            "\tgoods_name like concat('%',#{keyword},'%')\n" +
            "AND\n" +
            "\tdeleted = 0\n" +
            "GROUP BY\n" +
            "\tcreate_time DESC\n" +
            "LIMIT #{size} OFFSET #{offset}\n")
    List<GoodsListOutputDTO> getByKeyword(String keyword, Long size, Long offset);

    //分页查询
    @Select("SELECT\n" +
            "\tgoods_id,\n" +
            "\tgoods_name,\n" +
            "\tgoods_Type,\n" +
            "\tgoods_Braend,\n" +
            "\tgoods_Remain,\n" +
            "\tpurchase_price,\n" +
            "\tsell_price,\n" +
            "\tcreate_time\n" +
            "FROM\n" +
            "\tgoods\n" +
            "WHERE\n" +
            "\tdeleted = 0\n" +
            "LIMIT #{size} OFFSET #{offset}\n")
    List<GoodsListOutputDTO> getAllGoods(Long size,Long offset);

    //根据id查询
    @Select("SELECT\n" +
            "\tgoods_id,\n" +
            "\tgoods_name,\n" +
            "\tgoods_Type,\n" +
            "\tgoods_Braend,\n" +
            "\tgoods_Remain,\n" +
            "\tsell_price,\n" +
            "\tpurchase_price,\n" +
            "\tcreate_time\n" +
            "FROM\n" +
            "\tgoods\n" +
            "WHERE\n" +
            "\tgoods_id = #{goodsId}\n" +
            "AND\n" +
            "\tdeleted = 0\n")
    Goods getById(Long goodsId);

    //根据name查询
    @Select("SELECT\n" +
            "\tgoods_id,\n" +
            "\tgoods_name,\n" +
            "\tgoods_Type,\n" +
            "\tgoods_Braend,\n" +
            "\tgoods_Remain,\n" +
            "\tcreate_time\n" +
            "FROM\n" +
            "\tgoods\n" +
            "WHERE\n" +
            "\tgoods_name = #{goodName}\n" +
            "AND\n" +
            "\tdeleted = 0\n")
    GoodsListOutputDTO getByName(String goodName);

    //根据name查询id
    @Select("SELECT\n" +
            "\tgoods_id\n" +
            "FROM\n" +
            "\tgoods\n" +
            "WHERE\n" +
            "\tgoods_name = #{goodName}\n" +
            "AND\n" +
            "\tdeleted = 0\n")
    Long getGoodIdByName(String goodName);

@Update("UPDATE\n" +
        "\tgoods\n" +
        "SET\n" +
        "\tdeleted = 1,\n" +
        "\tdeleted_time = NOW()\n" +
        "WHERE\n" +
        "\tgoods_id = #{goodId}\n" +
        "AND\n" +
        "\tdeleted = 0\n")
        Integer logicDelete(Long goodId);



}
